package com.k.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.GoodOrder;
import com.k.demo.entity.RedBag;
import com.k.demo.entity.RedBagRecord;
import com.k.demo.service.RedBagRecordService;
import com.k.demo.service.RedBagService;

@RestController
@RequestMapping("/redbag")
public class RedBagController {
	
	@Autowired
	private RedBagService redBagService;
	
	@Autowired
	private RedBagRecordService redBagRecordService;

	
	//显示全部可领取的红包，对于已经领取过的红包则不显示
	//还需要加上一个与当前用户拥有的红包进行判断
	@RequestMapping("/getall")
	public Object getAll(HttpSession session) throws ParseException{
		Map<String,Object> map = new HashMap<String, Object>();
		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
		//此处的getAvailableRedBag是与当前用户拥有的红包进行比较  返回其没有的红包
		List<RedBag> redbaglist = redBagService.getAvailableRedBag(login_user.getUser_id());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间    
        Date date = new Date();
        String nowtime = df.format(date);
        // System.out.println(nowtime);
          //
        Date d1 = df.parse(nowtime);
        List<RedBag> list = new ArrayList<RedBag>();
        
		for(RedBag red : redbaglist) {
			Date d2 = df.parse(red.getDead_time());
			System.out.println("红包过期时间:"+red.getDead_time());
			long diff = d2.getTime() - d1.getTime();// 这样得到的差值是微秒级别
	        long days = diff / (1000 * 60 * 60 * 24);//天
	        System.out.println("红包还有几天过期:"+days);
			if(days > 0) {//如果没过期则加入到要传输的list中
				list.add(red);
			}
		}
		//先更新了再拿
		map.put("redbaglist",list);
		
		return map;
	}
	
	//红包管理的相关逻辑
	
	//新增一个红包
	@RequestMapping("/addredbag")
	public Object addRedBag(@RequestBody RedBag redBag) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("addResult", redBagService.addRedBag(redBag));
		
		return map;
		
	}
	
	//红包分页显示
	@RequestMapping("/allByPage")
	public Object getAllWithPage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("allByPage", redBagService.getAllWithPage((int)map.get("pageNum"),(int)map.get("pageSize")));
		
		return rmap;
		
	}
	
	//红包搜索结果分页显示
	@RequestMapping("/searchWithPage")
	public Object searchWithPage(@RequestBody RedBag redBag, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		System.out.println(redBag.getFace_value());
		System.out.println(redBag.getUser_level());
//		RedBag redBag = new RedBag();
//		//1判断红包id
//		if("".equals(map.get("red_bag_id")) || map.get("red_bag_id")==null ) {
//			redBag.setRed_bag_id(null);
//		}else {
//			redBag.setRed_bag_id(Integer.parseInt((String)map.get("red_bag_id")));
//		}
//		//2判断红包的面值
//		if(map.get("face_value").equals("") || map.get("face_value")==null ) {
//			redBag.setFace_value(null);
//		}else {
//			redBag.setFace_value((double)map.get("face_value"));
//		}
//		//3判断有效期
//		if("".equals(map.get("valid_time")) || map.get("valid_time")==null ) {
//			redBag.setValid_time(null);
//		}else {
//			redBag.setValid_time(Integer.parseInt((String)map.get("valid_time")));
//		}		
//		//4判断红包的最低使用限额
//		if("".equals(map.get("user_level")) || map.get("user_level")==null ) {
//			redBag.setUser_level(null);;
//		}else {
//			redBag.setUser_level(Integer.parseInt((String)map.get("user_level")));
//		}
//		//5判断最低领取等级
//		if("".equals(map.get("available_level")) || map.get("available_level")==null ) {
//			redBag.setAvailable_level(null);
//		}else {
//			redBag.setAvailable_level(Integer.parseInt((String)map.get("available_level")));
//		}		
//		
//		redBag.setCreate_time((String)map.get("create_time"));
//		redBag.setDead_time((String)map.get("dead_time"));
//		redBag.setRed_bag_content((String)map.get("red_bag_content"));
//		
		rmap.put("allByPage", redBagService.searchResultWithPage(pageNum,pageSize,redBag));		
		return rmap;
		
	}
	
}
